package com.miyake.client.tools;

import java.util.List;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.client.datamanage.NodeProperty;

public class PlotListDialog extends ItemListDialog {

	public PlotListDialog(String nodeKey) {
		super(nodeKey);
	}

	@Override
	protected ComplexPanel getHeader(String nodeKey) {
		VerticalPanel panel = new VerticalPanel();
		final ListBox combo = new ListBox();
		panel.add(combo);
		combo.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				getContentList(combo.getValue(combo.getSelectedIndex()));
			}
		});
		
		service.getRootNode(nodeKey, new AsyncCallback<NodeProperty>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(NodeProperty result) {
				service.getSpecifiedSubNodes(result.getKey(), NodeProperty.FOLDER|NodeProperty.DATA, new AsyncCallback<List<NodeProperty>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(List<NodeProperty> result) {
						for (NodeProperty n : result) {
							combo.addItem(n.getName(), n.getKey());
						}
					}
					
				});
			}	
		});
		
		
		return panel;
	}

	@Override
	protected void getContentList(String nodeKey) {	
		service.getPlotList(nodeKey, new AsyncCallback<List<NodeProperty>>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<NodeProperty> result) {
				showList(result);
			}
		});
	}

	@Override
	protected void getContent(String key, final Image image) {
		service.getPlotURL(key, 170, 100, new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(String result) {
				image.setUrl(result);
			}
		});
	}

}
